package com.sandfoxstudio.GPSTest;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;

public class GPSTestActivity extends MapActivity implements LocationListener {
	/** Called when the activity is first created. */
	LocationManager mLocationManager;
	TextView tv;
	Location mLocation;
	SQLiteDB dataBase;

	//Map
	private MapView mapView = null;
	private MyLocationOverlay locationOverlay = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		dataBase = new SQLiteDB(getApplicationContext());

		Button bt = (Button) findViewById(R.id.resetButton);
		bt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dataBase.deleteData();
			}
		});

		tv = (TextView) findViewById(R.id.tv1);
		mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_FINE);
		criteria.setPowerRequirement(Criteria.POWER_LOW);

		String locationProvider = mLocationManager.getBestProvider(criteria, true);
		mLocation = mLocationManager.getLastKnownLocation(locationProvider);
		//showupdate();
		Toast.makeText(getApplicationContext(), locationProvider, Toast.LENGTH_LONG).show();
		mLocationManager.requestLocationUpdates(locationProvider, 1000, (float) 0.0, this);

		//mapView setting 
		mapView = (MapView) findViewById(R.id.mapView);
		mapView.setBuiltInZoomControls(true);

		MapController mapController = mapView.getController();
		mapController.setZoom(16); //1~21

		//add compass locationOverlay = new
		locationOverlay = new MyLocationOverlay(this.getApplicationContext(), mapView);
		mapView.getOverlays().add(locationOverlay);
		locationOverlay.enableCompass();

	}

	public void showupdate() {
		tv.setText("Last location \nlat:" + mLocation.getLatitude() + "\nlong:" + mLocation.getLongitude() + "\nBearing:" + mLocation.getBearing());
		dataBase.addData(mLocation.getLatitude(), mLocation.getLongitude());
		GeoPoint point1e6 = getPoint(mLocation.getLatitude(), mLocation.getLongitude());
		//mapView.getController().setCenter(point1e6);
		mapView.getController().animateTo(point1e6);
	}

	@Override
	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub
		mLocation = location;
		showupdate();
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub

	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	private static GeoPoint getPoint(double lat, double lon) {
		return (new GeoPoint((int) (lat * 1000000.0), (int) (lon * 1000000.0)));
	}
}
